package com.youxin.cpcenter.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.ent.RequestEntMemberIdentifyCapitalBean;
import com.youxin.busfoundation.bean.leads.LeadsStatTotalBean;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.cpcenter.dao.TbEntMemberIdentifyCapitalMapper;
import com.youxin.cpcenter.dao.TbEntMemberInfoMapper;
import com.youxin.cpcenter.model.TbEntMemberIdentifyCapital;
import com.youxin.cpcenter.service.EntMemberIdentifyCapitalService;
import com.youxin.foundation.utils.DateUtil;

/**
 * 
 * @ClassName: EntMemberIdentifyCapitalServiceImpl 
 * @Description: 企业认证资本金券和可用线索等
 * @author yingde.cao
 * @date 2017年12月9日 下午3:25:19 
 *
 */
@Service("entMemberIdentifyCapitalService")
public class EntMemberIdentifyCapitalServiceImpl implements EntMemberIdentifyCapitalService {
	@Autowired
	private TbEntMemberIdentifyCapitalMapper tbEntMemberIdentifyCapitalMapper;
	@Autowired
	private TbEntMemberInfoMapper tbEntMemberInfoMapper;
	/**
	 * @Title: increaseCoupon 
	 * @Description: 添加企业金券
	 * @param @param entity
	 * @param @return
	 * @return ResponseObject
	 * @throws
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseObject increaseCoupon(RequestEntMemberIdentifyCapitalBean bean) {
		TbEntMemberIdentifyCapital model = new TbEntMemberIdentifyCapital();
		model.setId(bean.getEntIdentifyId());
		model.setCoupon(bean.getCoupon());
		model.setUpdateTime(System.currentTimeMillis());
		int row = tbEntMemberIdentifyCapitalMapper.increaseCoupon(model);
		if (row > 0) {
			return ResponseObject.successObject();
		}
		return ResponseObject.userFailure("更新失败");
	}
	/**
	 * @Title: increaseCoupon 
	 * @Description: 扣减企业金券
	 * @param @param entity
	 * @param @return
	 * @return ResponseObject
	 * @throws
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseObject decreaseCoupon(RequestEntMemberIdentifyCapitalBean bean) {
		TbEntMemberIdentifyCapital model = new TbEntMemberIdentifyCapital();
		UserInfo userInfo = LoginAuth.getUserInfo();
		model.setId(userInfo.getEntIdentifyId());
		model.setCoupon(bean.getCoupon());
		model.setUpdateTime((long)DateUtil.getDateForInt());
		int row = tbEntMemberIdentifyCapitalMapper.increaseCoupon(model);
		if (row > 0) {
			return ResponseObject.successObject();
		}
		return ResponseObject.userFailure("更新失败");
	}
	
	@Override
	public ResponseObject selectByPrimaryKey(RequestEntMemberIdentifyCapitalBean bean) {
		RequestEntMemberIdentifyCapitalBean rs = tbEntMemberIdentifyCapitalMapper.selectEntConsumDetails(bean.getEntIdentifyId());
		LeadsStatTotalBean totalObject=tbEntMemberInfoMapper.queryStatMemberNumber(bean.getEntIdentifyId());
		rs.setMemberNumber(Math.max(0, rs.getTotalMemberNumber().intValue()-totalObject.getTotal().intValue()));
		int remainingMonths = DateUtil.getRemainingMonths(DateUtil.dateToString(rs.getExpiresTime()),DateUtil.dateToString(new Date()));
		rs.setRemainingMonths(remainingMonths<0?0:remainingMonths);
		int remainingYears = DateUtil.yearDateDiff(DateUtil.dateToString(rs.getStartTime()),DateUtil.dateToString(rs.getExpiresTime()));
		rs.setRemainingYears(remainingYears<0?0:remainingYears);
		return ResponseObject.successObject(rs);
	}
}
